import { Good } from './goods.entity';
import {
  Controller,
  Post,
  Body,
  UseGuards,
  Request,
  Get,
  Delete,
  Put,
  Param,
  Query,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { JwtAuthGuardUser } from 'src/auth/guards/jwt-auth.guard';
import { Log } from 'src/libs/utils';
import { GoodsService } from './goods.service';
import { CreateGoodDto } from './dto/create-good.dto';
import { RemoveGoodDto } from './dto/remote-good.dto';
import { UpdateGoodDto } from './dto/update-good.dto';
import { RetrieveGoodDto } from './dto/retrieve-good.dto';
import { FindGoodDto } from './dto/find-good.dto';
@ApiTags('用户')
@Controller('api/goods')
export class GoodsController {
  constructor(private readonly goodsService: GoodsService) {}

  //增加商品
  @UseGuards(JwtAuthGuardUser)
  @Post()
  @ApiOperation({ summary: '增加' })
  async create(@Body() createGoodDto: CreateGoodDto): Promise<Good> {
    return await this.goodsService.create(createGoodDto);
  }

  //通过id删除商品
  @UseGuards(JwtAuthGuardUser)
  @Delete()
  @ApiOperation({ summary: '删除' })
  async remove(
    @Body() removeGoodDto: RemoveGoodDto,
    @Request() req,
  ): Promise<any> {
    Log({ req });
    return await this.goodsService.delete(removeGoodDto);
  }

  //跟新商品
  @UseGuards(JwtAuthGuardUser)
  @Put(':id')
  @ApiOperation({ summary: '更新' })
  async update(
    @Param() params: RetrieveGoodDto,
    @Body() updateGoodDto: UpdateGoodDto,
  ): Promise<any> {
    return await this.goodsService.update({
      id: params.id,
      updateGoodDto,
    });
  }

  //列表
  // @UseGuards(JwtAuthGuardUser)
  @Get()
  @ApiOperation({ summary: '列表' })
  async findAll(@Query() query: FindGoodDto): Promise<Good> {
    return await this.goodsService.findAll(query);
  }
}
